package com.systop.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.systop.entity.Order;
import com.systop.service.OrderService;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
// import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;
import java.util.List;

/**
 * @Description: TODO
 * @Author: jsj
 * @Date: 2022/6/17
 */

// @RabbitListener(queues = "order.release.order.queue")
@Component
public class OrderCloseListener implements ChannelAwareMessageListener {

    @Autowired
    OrderService orderService;


    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            Order order = JSON.parseObject(new String(message.getBody()), Order.class);
            //查询所有订单
            List<Order> orderList = orderService.list(new QueryWrapper<Order>().eq("order_no", order.getOrderNo()));
            if (orderList!=null && orderList.size()>0) {
                orderService.closeOrder(orderList);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
